﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_BaseIntroDAL : IDcms_BaseIntroDAL
    {
        public Dcms_BaseIntroModel AddModel(Dcms_BaseIntroModel model)
        {
            string sQLString = "insert into Dcms_BaseIntro(Intro_CateId,Intro_Title,Intro_State,Intro_Image,Intro_Content,Intro_Order,Intro_AddTime) values(@Intro_CateId,@Intro_Title,@Intro_State,@Intro_Image,@Intro_Content,@Intro_Order,@Intro_AddTime) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Intro_CateId", SqlDbType.Int), new SqlParameter("@Intro_Title", SqlDbType.VarChar), new SqlParameter("@Intro_State", SqlDbType.TinyInt), new SqlParameter("@Intro_Image", SqlDbType.Int), new SqlParameter("@Intro_Content", SqlDbType.Text), new SqlParameter("@Intro_Order", SqlDbType.Int), new SqlParameter("@Intro_AddTime", SqlDbType.DateTime) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Intro_CateId;
            cmdParms[2].Value = model.Intro_Title;
            cmdParms[3].Value = model.Intro_State;
            cmdParms[4].Value = model.Intro_Image;
            cmdParms[5].Value = model.Intro_Content;
            cmdParms[6].Value = model.Intro_Order;
            cmdParms[7].Value = model.Intro_AddTime;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Intro_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_BaseIntroModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_BaseIntroModel> list = new List<Dcms_BaseIntroModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_BaseIntroModel item = new Dcms_BaseIntroModel();
                if (ds.Tables[0].Rows[i]["Intro_Id"].ToString() != "") item.Intro_Id = int.Parse(ds.Tables[0].Rows[i]["Intro_Id"].ToString());
                if (ds.Tables[0].Rows[i]["Intro_CateId"].ToString() != "") item.Intro_CateId = int.Parse(ds.Tables[0].Rows[i]["Intro_CateId"].ToString());
                item.Intro_Title = ds.Tables[0].Rows[i]["Intro_Title"].ToString();
                if (ds.Tables[0].Rows[i]["Intro_State"].ToString() != "") item.Intro_State = int.Parse(ds.Tables[0].Rows[i]["Intro_State"].ToString());
                if (ds.Tables[0].Rows[i]["Intro_Image"].ToString() != "") item.Intro_Image = int.Parse(ds.Tables[0].Rows[i]["Intro_Image"].ToString());
                item.Intro_Content = ds.Tables[0].Rows[i]["Intro_Content"].ToString();
                if (ds.Tables[0].Rows[i]["Intro_Order"].ToString() != "") item.Intro_Order = int.Parse(ds.Tables[0].Rows[i]["Intro_Order"].ToString());
                if (ds.Tables[0].Rows[i]["Intro_AddTime"].ToString() != "") item.Intro_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Intro_AddTime"].ToString());
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return SqlHelper.ExecuteSql("delete from Dcms_BaseIntro where Intro_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_BaseIntro where Intro_Id=@Intro_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Intro_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_BaseIntroModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public Dcms_BaseIntroModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_BaseIntro where Intro_Id=@Intro_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Intro_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_BaseIntroModel model = new Dcms_BaseIntroModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_BaseIntroModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_BaseIntro";
            return SqlHelper.CreateDataSet(sql);
        }

        public Dcms_BaseIntroModel Update(Dcms_BaseIntroModel model)
        {
            string sQLString = "update Dcms_BaseIntro set Intro_CateId=@Intro_CateId,Intro_Title=@Intro_Title,Intro_State=@Intro_State,Intro_Image=@Intro_Image,Intro_Content=@Intro_Content,Intro_Order=@Intro_Order,Intro_AddTime=@Intro_AddTime where Intro_Id=@Intro_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Intro_CateId", SqlDbType.Int), new SqlParameter("@Intro_Title", SqlDbType.VarChar), new SqlParameter("@Intro_State", SqlDbType.TinyInt), new SqlParameter("@Intro_Image", SqlDbType.Int), new SqlParameter("@Intro_Content", SqlDbType.Text), new SqlParameter("@Intro_Order", SqlDbType.Int), new SqlParameter("@Intro_AddTime", SqlDbType.DateTime), new SqlParameter("@Intro_Id", SqlDbType.Int) };
            cmdParms[0].Value = model.Intro_CateId;
            cmdParms[1].Value = model.Intro_Title;
            cmdParms[2].Value = model.Intro_State;
            cmdParms[3].Value = model.Intro_Image;
            cmdParms[4].Value = model.Intro_Content;
            cmdParms[5].Value = model.Intro_Order;
            cmdParms[6].Value = model.Intro_AddTime;
            cmdParms[7].Value = model.Intro_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return SqlHelper.ExecuteSql(string.Concat(new object[] { "Update Dcms_BaseIntro Set Intro_State=", StateValue, " where Intro_Id In (", AllID, ")" }), null) > 0;
        }
    }
}

